package com.cr.admin.system.controller;

import com.cr.admin.system.pojo.dto.SysRoleAddParam;
import com.cr.admin.system.pojo.dto.SysRoleQuery;
import com.cr.admin.system.pojo.dto.SysRoleUpdateParam;
import com.cr.admin.system.pojo.entity.SysRole;
import com.cr.admin.system.pojo.vo.SysRoleDetailVO;
import com.cr.admin.system.service.ISysRoleService;
import com.cr.common.response.PageData;
import com.cr.common.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "40.角色管理")
@RestController
@RequestMapping("/api/sys/roles")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @ApiOperation(value = "新增角色")
    @ApiOperationSupport(order = 100)
    @PostMapping
    public JsonResult add(@Validated @RequestBody SysRoleAddParam sysRoleAddParam) {
        sysRoleService.insert(sysRoleAddParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "删除角色")
    @ApiOperationSupport(order = 200)
    @DeleteMapping
    public JsonResult delete(@RequestParam("ids") List<Long> ids) {
        sysRoleService.deleteByIds(ids);
        return JsonResult.success();
    }

    @ApiOperation(value = "修改角色")
    @ApiOperationSupport(order = 300)
    @PutMapping
    public JsonResult update(@Validated @RequestBody SysRoleUpdateParam sysRoleUpdateParam) {
        sysRoleService.updateById(sysRoleUpdateParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "查询角色详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        SysRoleDetailVO sysRoleDetailVO = sysRoleService.selectById(id);
        return JsonResult.success(sysRoleDetailVO);
    }

    @ApiOperation(value = "查询角色列表")
    @ApiOperationSupport(order = 410)
    @GetMapping
    public JsonResult list(Integer pageNum, Integer pageSize, SysRoleQuery query) {
        PageData<SysRole> pageData = PageData.page(pageNum, pageSize, () -> sysRoleService.selectList(query));
        return JsonResult.success(pageData);
    }

    @ApiOperation(value = "查询全部角色")
    @ApiOperationSupport(order = 420)
    @GetMapping("/all")
    public JsonResult all() {
        List<SysRole> list = sysRoleService.selectList(null);
        return JsonResult.success(list);
    }

}
